package edu.scau.cxq.gp.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.entity.FinancialInfo;
import edu.scau.cxq.gp.entity.GoodsSellsInfo;
import edu.scau.cxq.gp.entity.RecordInOutInfo;
import edu.scau.cxq.gp.service.IFinancialInfoService;
import edu.scau.cxq.gp.service.IGoodsSellsInfoService;
import edu.scau.cxq.gp.service.IRecordInOutInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description 统计功能 前端控制器
 * @return
 * @author TLC
 */

@RestController
@ResponseResult
@RequestMapping("/statistic")
public class StatisticController {

    @Resource
    private IRecordInOutInfoService recordInOutInfoService;

    @Resource
    private IGoodsSellsInfoService goodsSellsInfoService;

    @Resource
    private IFinancialInfoService financialInfoService;

    @GetMapping ("/getRecordInOutInfos")
    public Map<String, Object> getRecordInOutInfos (@RequestParam("statisticalType") String statisticalType) {
        QueryWrapper<RecordInOutInfo> recordInInfoQueryWrapper = new QueryWrapper<>();
        recordInInfoQueryWrapper.eq("statistical_type", statisticalType + "_in").orderByAsc("statistical_time");
        List<RecordInOutInfo> in = recordInOutInfoService.list(recordInInfoQueryWrapper);

        QueryWrapper<RecordInOutInfo> recordOutInfoQueryWrapper = new QueryWrapper<>();
        recordOutInfoQueryWrapper.eq("statistical_type", statisticalType + "_out").orderByAsc("statistical_time");
        List<RecordInOutInfo> out = recordInOutInfoService.list(recordOutInfoQueryWrapper);

        JSONArray recordIn = new JSONArray();
        recordIn.addAll(in);

        JSONArray recordOut = new JSONArray();
        recordOut.addAll(out);

        Map<String, Object> result = new HashMap<>();

        result.put("recordIn", recordIn);
        result.put("recordOut", recordOut);

        return result;
    }

    @GetMapping ("/getFinancialInfo")
    public FinancialInfo getFinancialInfo (@RequestParam("recordYear") String recordYear) {
        QueryWrapper<FinancialInfo> record_year = new QueryWrapper<>();
        record_year.eq("record_year", recordYear);
        FinancialInfo financialInfo = financialInfoService.getOne(record_year);

        return financialInfo;
    }

    @GetMapping("/getGoodsSellsInfo")
    public JSONArray getGoodsSellsInfo() {

        QueryWrapper<GoodsSellsInfo> goodsSellsInfoQueryWrapper = new QueryWrapper<>();
        goodsSellsInfoQueryWrapper.orderByDesc("out_count");
        List<GoodsSellsInfo> list = goodsSellsInfoService.list(goodsSellsInfoQueryWrapper);
        list = list.subList(0, 5);

        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(list);

        return jsonArray;
    }

    @GetMapping("/getFinancialType")
    public JSONArray getFinancialType() {
        List<FinancialInfo> list = financialInfoService.list(null);

        List<String> recordYear = new ArrayList<>();

        for (FinancialInfo financialInfo : list) {
            recordYear.add(financialInfo.getRecordYear());
        }

        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(recordYear);

        return jsonArray;
    }

}
